package com.hoyin.viewtest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.hoyin.viewtest.dependencies.ImageListAdapter;
import com.hoyin.viewtest.dependencies.ListItem;

import java.util.ArrayList;
import java.util.List;

public class ListViewActivity extends AppCompatActivity {
	private List<ListItem> listItems = new ArrayList<>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_view);
		initItems(listItems);
		ImageListAdapter arrayAdapter = new ImageListAdapter(ListViewActivity.this, R.layout.list_item_layout, listItems);
		ListView listView = findViewById(R.id.listView);
		listView.setAdapter(arrayAdapter);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
				ListItem item = listItems.get(position);
				Toast.makeText(ListViewActivity.this, item.getName(), Toast.LENGTH_SHORT).show();
			}
		});
	}

	private void initItems(List<ListItem> listItems) {
		for (int i = 0; i < 10; i++) {
			listItems.add(new ListItem("Item" + i, R.drawable.list_item_image));
		}
	}
}